package member

import (
	"parking-lot-go-dingding/controller"
	"parking-lot-go-dingding/model/cars"
	"parking-lot-go-dingding/model/dingtalk/system"
	"parking-lot-go-dingding/model/member"
	"parking-lot-go-dingding/model/view"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
)

func TemporaryUpdate(c *gin.Context) {
	requestStruct := struct {
		AvailableAmount string   `json:"available_amount"`
		CarInfoList     []string `json:"car_info_list"`
		CarNoColorList  []int    `json:"car_no_color_list"`
		CarNumberList   []string `json:"car_number_list"`
		CarTypeList     []int    `json:"car_type_list"`
		DingtalkCorpId  string   `json:"dingtalk_corp_id"`
		ExpiresTime     string   `json:"expires_time"`
		MemberType      int      `json:"member_type"`
		Mobile          string   `json:"mobile"`
		RealName        string   `json:"real_name"`
		Remarks         string   `json:"remarks"`
		Id              int      `json:"id"`
	}{}

	//接收加密串
	if err := c.ShouldBindJSON(&requestStruct); err != nil {
		controller.Fail(c, "json接收失败"+err.Error())
		return
	}

	//查询当前会员信息
	var membersDingtalkViewStruct view.MembersDingtalkViewStruct
	memberFind, err := membersDingtalkViewStruct.First(map[string]interface{}{
		"member_id": requestStruct.Id,
	})
	if err != nil {
		controller.Fail(c, "会员信息查询失败"+err.Error())
		return
	}

	//余额有变动
	var dingtalkSystemCorpStruct system.DingtalkSystemCorpStruct
	//企业余额判断
	dingtalkSystemCorp, err := dingtalkSystemCorpStruct.First(requestStruct.DingtalkCorpId)
	if err != nil {
		controller.Fail(c, "企业信息获取失败"+err.Error())
		return
	}
	availableAmountFloat, _ := strconv.ParseFloat(requestStruct.AvailableAmount, 64)
	if memberFind.MemberMoney < availableAmountFloat { //给会员增加余额了
		if dingtalkSystemCorp.Money < availableAmountFloat-memberFind.MemberMoney {
			controller.Fail(c, "可划拨余额不足")
			return
		}
	}

	//查询车牌
	var carsStruct cars.CarsStruct
	cars, err := carsStruct.List([]interface{}{
		[]interface{}{"car_number", "in", requestStruct.CarNumberList},
		[]interface{}{"member_id", "!=", requestStruct.Id},
	})
	if err != nil {
		controller.Fail(c, "车辆信息查询失败")
		return
	}

	if len(cars) > 0 {
		carRepeat := ""
		for _, v := range cars {
			carRepeat += v.CarNumber + ","
		}
		carRepeat = strings.TrimRight(carRepeat, ",")
		controller.Fail(c, "车牌"+carRepeat+"已经注册过")
		return
	}

	expTime, _ := controller.StringToDate(requestStruct.ExpiresTime, "2006-01-02 15:04")
	//更新会员
	var memberStruct member.MemberStruct
	money, _ := strconv.ParseFloat(requestStruct.AvailableAmount, 64)
	if err := memberStruct.DingTalkUpdate(
		member.MemberStruct{
			Id:          requestStruct.Id,
			ExpiresTime: expTime,
			Remarks:     requestStruct.Remarks,
			UpdatedAt:   time.Now(),
			Money:       money,
		},
		requestStruct.CarInfoList,
		requestStruct.CarNumberList,
		requestStruct.CarTypeList,
		requestStruct.CarNoColorList,
		requestStruct.ExpiresTime,
		requestStruct.DingtalkCorpId,
		availableAmountFloat,
		dingtalkSystemCorp.Money); err != nil {
		controller.Fail(c, "会员修改失败")
		return
	}

	controller.Success(c, "会员修改成功")
}
